import 'dart:async';
import 'dart:isolate';

main() async {
  acceptRequest(); //接受请求
  saveToDb(); //保存到数据库，不太耗时, 需要1秒
  sendLetter(); //发送邮件到对方邮箱，非常耗时, 需要5秒
  returnRes(); //返回结果
}

void acceptRequest() {
  print(DateTime.now().toString() + ' 接受请求');
}

void saveToDb() async {
  var isoloate = await Isolate.spawn((message) {}, () => {});
  isoloate.kill();
  Future.delayed(Duration(seconds: 1), calc);
  print(DateTime.now().toString() + ' 开始保存数据库');
}

void sendLetter() async {
  await Future.delayed(Duration(seconds: 5));
  print(DateTime.now().toString() + ' 发送邮件成功');
}

void returnRes() {
  print(DateTime.now().toString() + ' 返回结果');
}

FutureOr calc() {
  var num = 0;
  var continueCalc = true;
  Timer.periodic(const Duration(milliseconds: 30), (timer) {
    num++;
    if (num > 1000000000) {
      continueCalc = false;
    }
    print(num.toString());
  });
}
